import { useFavicon } from '@vueuse/core'
import { defineStore } from 'pinia'

import apis from '@/apis'
import type { SystemStore } from '@/types/store/system'
export const systemStore = defineStore({
  id: 'systemStore',
  state: (): SystemStore => ({
    domain: '',
    logoPath: '',
    title: ''
  }),
  actions: {
    getSignData() {
      apis.user.signIn().then(res => {
        this.domain = res.data?.domain || ''
      })
    },

    getSiteInfo() {
      apis.user.support().then(res => {
        const icon = useFavicon()
        // const siteTitle = useTitle()
        this.logoPath = res.data?.website_logo ?? ''
        this.title = res.data?.website_title ?? ''
        const domain = import.meta.env.VITE_BASE_URL || window.location.origin
        // siteTitle.value = this.title
        icon.value =
          domain && this.logoPath ? `${domain}${this.logoPath}` : '/favicon.ico'
      })
    }
  },
  persist: {
    key: 'WEB_CDN_WEBSITE_DATA',
    storage: sessionStorage
  }
})
